package com.meal.base.service.message.impl;

import com.meal.base.service.message.MailService;
import com.meal.common.model.Result;
import com.meal.entity.base.BaseController;
import com.meal.entity.model.tool.User;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;

/**
 * @program: meal->MailServiceImpl
 * @description: 邮件实现类
 * @author: yangzhanghui
 * @create: 2020-04-11 21:55
 **/
@Service
public class MailServiceImpl extends BaseController implements MailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @SneakyThrows
    public Result sendMail(User user) {
        StringBuilder stringBuilder=new StringBuilder();

        stringBuilder.append("<html><head><title></title></head><body>");
        stringBuilder.append("亲爱的用户"+user.getAccount()+",您的当前密码是："+user.getPassword());
        stringBuilder.append("</br><a href='https://www.baidu.com/'>访问百度</a></body><html>");
        MimeMessage mimeMessage=javaMailSender.createMimeMessage();
        //multipart模式
        try {
            MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setTo(user.getMail());//收件人邮箱user.getMail()
            mimeMessageHelper.setFrom("sple_vip@163.com");//发件人邮箱
            mimeMessage.setSubject("找回密码");
            //启用html
            mimeMessageHelper.setText(stringBuilder.toString(),true);
            javaMailSender.send(mimeMessage);
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return FAIL;
        }
    }
}